home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Helpers / Tzu Release 4 / User Tzus / Comment.cp < prev    next >
Encoding:
Text File  |  1994-11-03  |  1.8 KB  |  94 lines  |  [TEXT/MMCC]

  1.  
  2. // * Comment Tzu
  3. // * ©1994 Michael A. Kelly.  All Rights Reserved.
  4. // * 
  5. // * 100 11 Oct 94 ckt
  6. // * 101 13 Oct 94 ckt - rewrote to comment more intelligently
  7. // *                         & wrote InsertText & comment
  8. // * 102 27 Oct 94 mak - rewrote to comment more intelligently
  9. // *                         and to comment and uncomment with the same command, ala CMaster
  10.  
  11. #include "TzuTools.h"
  12.  
  13.  
  14.  
  15. pascal void main(
  16.     Handle        text );
  17.  
  18.  
  19.  
  20. pascal void main(
  21.     Handle        text )
  22. {
  23.  
  24.     EnterCodeResource();
  25.     
  26.     long        size;
  27.     
  28.     HUnlock( text );
  29.  
  30.     if ( (*text)[0] == '/' && (*text)[1] == '/' ) {
  31.  
  32.         const char    *commentText = "\r";
  33.         Str15        findText = "\p\r//";
  34.         Handle        commentStr = NewHandleClear( 1 );
  35.  
  36.         BlockMoveData( commentText, *commentStr, 1 );
  37.     
  38.         size = GetHandleSize( text );
  39.         BlockMove( *text + 2, *text, size - 2 );
  40.         SetHandleSize( text, size - 2 );
  41.  
  42.         if ( ReplaceText( text, commentStr, findText ) < 0 ) {
  43.             SysBeep(0);
  44.         }
  45.  
  46.         DisposeHandle( commentStr );
  47.  
  48.     } else {
  49.  
  50.         const char    *commentText = "\r//";
  51.         Str15        findText = "\p\r";
  52.         Handle        commentStr = NewHandleClear( 3 );
  53.         Boolean        endReturn;
  54.  
  55.         BlockMoveData( commentText, *commentStr, 3 );
  56.     
  57.         size = GetHandleSize( text );
  58.         SetHandleSize( text, size + 2 );
  59.         if ( MemError() ) {
  60.             SysBeep(0);
  61.             DisposeHandle( commentStr );
  62.             goto ret;
  63.         }
  64.         BlockMove( *text, *text + 2, size );
  65.         (*text)[0] = (*text)[1] = '/';
  66.         if ( (*text)[size + 1] == '\r' ) {
  67.             SetHandleSize( text, size + 1 );
  68.             endReturn = (MemError() == noErr);
  69.         } else {
  70.             endReturn = false;
  71.         }
  72.         if ( ReplaceText( text, commentStr, findText ) < 0 ) {
  73.             SysBeep(0);
  74.         }
  75.         if ( endReturn ) {
  76.             size = GetHandleSize( text );
  77.             SetHandleSize( text, size + 1 );
  78.             if ( MemError() == noErr ) {
  79.                 (*text)[size] = '\r';
  80.             }
  81.         }
  82.  
  83.         DisposeHandle( commentStr );
  84.     
  85.     }
  86.  
  87.  
  88. ret:
  89.  
  90.     HLockHi( text );
  91.     
  92.     ExitCodeResource();
  93.  
  94. }